<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>预警管理</title>
		<link href="css/mui.min.css" rel="stylesheet" type="text/css">
		<link href="css/new_file.css" rel="stylesheet" type="text/css">
		<link rel="stylesheet" type="text/css" href="css/app.css" />
		<link rel="stylesheet" type="text/css" href="css/mui.picker.min.css" />

		<script charset="UTF-8" src="js/config.js"></script>
		<script charset="UTF-8" src="js/testActivityName.js"></script>
		<script charset="UTF-8" src="js/core.js"></script>
		<script charset="UTF-8" src="js/cipher-core.js"></script>
		<script charset="UTF-8" src="js/tripledes.js"></script>
		<script charset="UTF-8" src="js/mode-ecb.js"></script>
		<script src="js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript" src="js/zepto.min.js"></script>
		<script type="text/javascript" src="js/spin.min.js"></script>
		<script charset="UTF-8" src="js/mui.min.js"></script>
		<script src="js/jquery.qrcode.min.js"></script>
		<script src="js/qrcode.js"></script>
		<script src="js/jquery.qrcode.js"></script>
		<script src="js/mui.picker.min.js"></script>

	</head>
	<style>
		.mui-bar{
			     padding-right: 0px; 
			     padding-left:0px; 
		}
	</style>
	<body>

		
				<div class="head-zhjf">
					<img src="images/fanhui@3x.png" class="ryjk-tx" onclick="alarmfanhui()">
					<div id="zhaoniuBut">
					</div>
					<p style="color: white;">找牛</p>
				</div>
			
				<div class="search-div">
					<div class="g-bd2">
						<div class="search-l-div">
							<div class="seach-l-a">
								<img class="sousuo-acx" src="images/sousuo-btn@3x.png">
								<input id="CattleSeachDataCowshed" class="xtsz-search-btn" type="text" placeholder="请输入搜索内容" onfocus="if(value==defaultValue){value='';this.style.color='#393e46'}" onblur="if(!value){value=defaultValue;this.style.color='#cdcdcd'}">
			
							</div>
						</div>
						<div class="seach-r-div" onclick="CowshedButID()">
							<img class="cx-div" src="images/sousuo-a-btn@3x.png" />
						</div>
					</div>
				</div>
		<div  class="clear"></div>
		<div id="noApplicationRecord">
			<div style="width: 100%;height: 100%;text-align: center;margin-top: 45%;">
				<img src="images/nodata.png" style="width: 200px;height: 140px;"/>
			</div>
		</div>
		<div id="hddb" style="position: fixed;right: -63px;bottom: 30px;border-radius: 25px;width: 50px;height: 50px;text-align: center;">
			<img src="img/hddb.png" style="width: 32px;height: 32px;    margin-top: 9px;" onclick="goTop()">
		</div>
		<script>
			var BatchStr =null;
			var BatchArr = new Array();
			var findCowArr = new Array();
			var times;
			var noApplicationRecord = document.getElementById('noApplicationRecord');
			var zhaoniuBut = document.getElementById('zhaoniuBut');
			var alarmData = JSON.parse(localStorage.getItem('alarmData'));
			console.log(alarmData);

			function setDiv(item) {
//				alert(item.AllowLightTime);
				if(item.AllowLightTime == null) {
					var div = '<div  id="' + item.cowID + '" " onclick="FindACow()"><div class="liebiao-yujin"><div class="div-yujin-top"><div class="yujin-t-l">'					
						+'</div><div class="zhaon-btn-s" id="s' + item.cowID + '"  >找牛</div><div class="clear"></div></div><div><div class="div-yujin-a"><div class="jinage"></div><p style="margin-bottom: 8px;">奶牛耳号</p><div>' +
						item.cowCode +'</div>'+
						item.wrongHouse +
						'</span></div><div class="div-yujin-a"><div class="jinage"></div><p style="margin-bottom: 8px;">所属牛舍</p><span class="lanhao-zq">' +
						item.rightHouse +
						'</span></div><div class="div-yujin-a"><p>亮灯倒计时</p><p class="mt10">' +
						item.alarmTime +
						'</p></div><div class="clear"></div></div><div class="clear"></div></div></div>'
				} else {
					var div = '<div data-options=\'{"beginYear":' +
						parseInt(item.AllowLightTime.substr(0, 4)) +
						',"beginMonth":' +
						parseInt(item.AllowLightTime.substr(5, 2)) +
						',"beginDay":' +
						parseInt(item.AllowLightTime.substr(8, 2)) +
						',"beginHours":' +
						parseInt(item.AllowLightTime.substr(11, 2)) +
						'}\' class="btn mui-btn mui-btn-block" style="padding: 0;border: none; " id="' + item.cowID + '" ><div class="liebiao-yujin"><div class="div-yujin-top"><div class="yujin-t-l">' +
					    '</div><div class="zhaon-btn-s" id="s' + item.cowID + '"  >找牛</div><div class="clear"></div></div><div><div class="div-yujin-a"><div class="jinage"></div><p style="margin-bottom: 8px;">奶牛耳号</p><div>' +
						item.cowCode +'</div>'+
						item.wrongHouse +
						'</span></div><div class="div-yujin-a"><div class="jinage"></div><p style="margin-bottom: 8px;">所属牛舍</p><span class="lanhao-zq">' +
						item.rightHouse +
						'</span></div><div class="div-yujin-a"><p>亮灯倒计时</p><p class="mt10">' +
						item.alarmTime +
						'</p></div><div class="clear"></div></div><div class="clear"></div></div></div>'
				}
				return div
			}
			function getnoApplicationData() {
				var html = ''
				for(var i = 0; i < alarmData.length; i++) {
					html += setDiv(alarmData[i])
				}
				noApplicationRecord.innerHTML = html
			}
			if(alarmData.length != 0){
				window.onload = getnoApplicationData()
			}
			
			
			var findNumber = 1;
			//初步提出可批量找牛的ID
			for(var i = 0; i < alarmData.length; i++) {
				if(alarmData[i].countDown != undefined) {
//					findCowArr.push(alarmData[i].cowID);
					resetTime('s' + alarmData[i].cowID, alarmData[i].countDown);
				} else if(alarmData[i].AllowLightTime != null) {
					BatchArr.push(alarmData[i].cowID);
					BatchStr = alarmData[i].AllowLightTime;
				}else{
					findCowArr.push("\n"+findNumber+"、"+alarmData[i].cowCode);
					findNumber++;
				}
			}

			
			function setDiv1(item) {
				if(item == null) {
					var div = '<div id="dome1" onclick="FindACow1()" style="background: none;float:  right;width:  auto;border: none;font-size: 16px;"><div class="head-zhjf-img"><p style="color: white;margin-top: -3px;">批量找牛</p></div></div>'
				} else {
					var div = '<div id="dome1" data-options=\'{"beginYear":' +
						parseInt(item.substr(0, 4)) +
						',"beginMonth":' +
						parseInt(item.substr(5, 2)) +
						',"beginDay":' +
						parseInt(item.substr(8, 2)) +
						',"beginHours":' +
						parseInt(item.substr(11, 2)) +
						'}\' class="btn mui-btn mui-btn-block" style="background: none;float:  right;width:  auto;border: none;font-size: 16px;"><div class="head-zhjf-img"><p style="color: white;margin-top: 4px;">批量找牛</p></div>'
				}
				return div
			}
			function getnoApplicationData1() {
				var html = ''
				html += setDiv1(BatchStr)
				
				zhaoniuBut.innerHTML = html
			}
			window.onload = getnoApplicationData1()


			var cows;
			var time;

			//			function findCowliebiao(id) {
			//				alert(2)
			//				BatchSearch == 0;
			//				
			//			}
			(function($) {
				$.init();
				var btns = $('.btn');
				btns.each(function(i, btn) {
					
					btn.addEventListener('tap', function() {
						var _self = this;
						var optionsJson = this.getAttribute('data-options') || '{}';
						var options = JSON.parse(optionsJson);
						var id = this.getAttribute('id');
						_self.picker = new $.DtPicker(options);
						_self.picker.show(function(rs) {
							console.log(id);
							cows = id
//							alert(cows);
							time = rs.text;
							if(id == "dome1") {
								BatchSearchForCattle();
							} else {
								findCowBut();
							}
							_self.picker.dispose();
							_self.picker = null;
						});
					}, false);
				});
			})(mui);

			function findCowBut() {
				var str = $("#s" + cows).html();
				if(str == "找牛") {
					var keys = JSON.parse(localStorage.getItem('key'));
					var arr = new Array();
					arr.push(cows);
					var map = {};
					map.key = keys;
					map.cows = arr;
					map.time = time;
					map.phoneType = "android";
					map.language = "zh";
					var jsonStr = JSON.stringify(map);
					console.log(jsonStr);
					$.ajax({
						type: "POST",
						url: url_prefix + "findCow",
						dataType: 'JSON',
						data: {
							'request': encryptByDES(jsonStr)
						},
						success: function(data) {

							var str1 = decryptByDESModeEBC(data["result"]); //解密
							var params = str1.replace(/\+/g, " "); //去掉字符串里的+号
							var str2 = UTFTranslate.ReChange(chineseFromUtf8Url(params)); //转utf8
							var datas = JSON.stringify(str2); //转JSON字符串
							var aa = datas.replace(/\\n/g, " "); //去掉换行
							var objs = eval('(' + aa + ')');
							var bb = eval('(' + objs + ')');
							console.log(bb);
							if(bb.head.status == 1) {
								for(var i = 0; i < BatchArr.length; i++) {
									if(BatchArr[i] == cows) {
										BatchArr.splice(i, 1);
									}
								}
//								bb.data.returnList[0].Countdown = 69;
								times = parseInt(bb.data.returnList[0].Countdown);
								resetTime('s' + cows, times);
							} else {
								mui.alert(bb.head.info);
							}
						}
					});
				}
			}
			
			function FindACow() {
				mui.alert("设备未上线，无法预约找牛");
			}
			function FindACow1() {
				mui.alert("设备未上线，无法批量预约找牛");
			}
			

			function BatchSearchForCattle() {
				BatchSearch = 0;
				console.log(findCowArr);
				console.log(BatchArr);
				if(BatchArr.length != 0) {
					console.log(BatchArr);
					var keys = JSON.parse(localStorage.getItem('key'));
					var map = {};
					map.key = keys;
					map.cows = BatchArr;
					map.time = time;
					map.phoneType = "android";
					map.language = "zh";
					var jsonStr = JSON.stringify(map);
					$.ajax({
						type: "POST",
						url: url_prefix + "findCow",
						dataType: 'JSON',
						data: {
							'request': encryptByDES(jsonStr)
						},
						success: function(data) {
							var str1 = decryptByDESModeEBC(data["result"]); //解密
							var params = str1.replace(/\+/g, " "); //去掉字符串里的+号
							var str2 = UTFTranslate.ReChange(chineseFromUtf8Url(params)); //转utf8
							var datas = JSON.stringify(str2); //转JSON字符串
							var aa = datas.replace(/\\n/g, " "); //去掉换行
							var objs = eval('(' + aa + ')');
							var bb = eval('(' + objs + ')');
							if(bb.head.status == 1) {
								
								if (findCowArr.length != 0) {
									mui.alert(findCowArr +"\n以上编号的奶牛因设备未上线，无法预约找牛");
								}
								
								console.log(bb.data.returnList);
//								bb.data.returnList = [{"Countdown": 12},{"Countdown": 12},{"Countdown": 67}]
								for(var i = 0; i < BatchArr.length; i++) {
									times = parseInt(bb.data.returnList[i].Countdown);
									resetTime('s' + BatchArr[i], times);
								}
							} else {
								mui.alert(bb.head.info);
							}
						}
					});
				} else {
					mui.alert("无牛可找");
				}
			}

			function alarmfanhui() {
				window.location = "功能列表.html";
			}

			function resetTime(id, times) {  
				var timer = null;  
				var t = times; 
				console.log(t); 
				var h = 0; 
				var m = 0;  
				var s = 0; 
				h = parseInt(t / 3600)
				m = parseInt(t / 60 % 60);  
				m < 10 && (m = '0' + m);  
				s = Math.floor(t % 60);  
				function countDown() {   
					s--;   
					s < 10 && (s = '0' + s);  
					if(s.length >= 3) {  
						if(Number(m) == 0) {
							s = 59; 
							m = 59;   
							h = (Number(h) - 1);
						} else {
							s = 59;    
							m = (Number(m) - 1);
						}  
					}
					if(m < 10) {
						m = "0" + Number(m);
					}
//					if(h.length >= 3) { 
//						h = '0';    
//						m = '00';    
//						s = '00';  
//						console.log(h)  
//						console.log(h.length); 
//						clearInterval(timer);   
//					}   
					console.log(h + ":" + m + ":" + s);
					console.log(id);
					$("#" + id).html(h + ":" + m + ":" + s); 
					if(s == '00' && m == '00' && h == '0') {
						clearInterval(timer);
						$("#" + id).html("已亮灯");
						return;
					}
				}  
				timer = setInterval(countDown, 1000);
			}
			function CowshedBut(id) {
				localStorage.setItem('alarmID', JSON.stringify(id));
				var houseID = cowLostData[id]["houseID"];
				var keys = JSON.parse(localStorage.getItem('key'));
				var map = {};
				map.key = keys;
				map.houseID = houseID;
				map.phoneType = "android";
				map.language = "zh";
				var jsonStr = JSON.stringify(map);
				$.ajax({
					type: "POST",
					url: url_prefix + "alarm",
					dataType: 'JSON',
					data: {
						'request': encryptByDES(jsonStr)
					},
					success: function(data) {
						var str1 = decryptByDESModeEBC(data["result"]); //解密
						var params = str1.replace(/\+/g, " "); //去掉字符串里的+号
						var str2 = UTFTranslate.ReChange(chineseFromUtf8Url(params)); //转utf8
						var datas = JSON.stringify(str2); //转JSON字符串
						var aa = datas.replace(/\\n/g, " "); //去掉换行
						var objs = eval('(' + aa + ')');
						var bb = eval('(' + objs + ')');
						if(bb.head.status == 1) {
							console.log(bb.data);
							localStorage.setItem('alarmData', JSON.stringify(bb.data));
							window.location = "5.预警管理.html";
						} else {
							mui.alert(bb.head.info);
						}
					}
				});
			
			}
			function goTop(){
						    //速度
						    var speed = 100;
						    //开启定时器
						    
						    var timer = setInterval(function(){
						        //获取滚动条的高度
						        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
						        //设置滚动的高度
						        document.documentElement.scrollTop = document.body.scrollTop = (scrollTop - speed);
						        //清除定时器
						        speed += 30;
						        if(scrollTop <= 0){
						            clearInterval(timer);
						        }
						    },30)
						}
						
			function showLogin(){  
						    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
						    if (scrollTop > 0) {
						    	$("#hddb").css("right","-3px");
						    } else{
						    	$("#hddb").css("right","-63px");
						    } 
						    //每秒执行一次,showTime()  
						    setTimeout("showLogin()",500);  
						}
						showLogin()
		</script>

	</body>

</html>